package com.wheesper.www.WheesperServer.Common;

import java.util.Set;
import java.util.TreeSet;
import ProtocolBuffer.ProtoMessageOuterClass.*;

public class ContactingRoutine{
	public enum Status{
		APPLYING,
		WAITTING_FOR_REPLY,
		FINISHED_WITH_SUCCESS,
		FINISHED_WITH_FAILURE,
		ERROR
	}
	private Status _status;
	private final String _applyer;
	private final String _target;
	public ContactingRoutine(String applyer,String target){
		this._applyer = applyer;
		this._target = target;
		this._status = Status.APPLYING;
	}
	
	@Override
	public boolean equals(Object routine) {
		return 
				((this._applyer == ((ContactingRoutine)routine)._applyer)
						&&
						( this._target== ((ContactingRoutine)routine)._target));
	}
	
	public ContactingRoutine NextStatus(ContactApplyRequest response) {
		if(this.status() != Status.APPLYING) {
			this._status = Status.ERROR;
			return this;
		}
		this._status = Status.WAITTING_FOR_REPLY;
		return this;
	}
	
	public ContactingRoutine NextStatus(ContactReplyRequest response) {
		if(this.status() != Status.WAITTING_FOR_REPLY) {
			this._status = Status.ERROR;
			return this;
		}
		boolean isAccepted = response.getIsAccepted();
		if(isAccepted)
			this._status = Status.FINISHED_WITH_SUCCESS;
		else
			this._status = Status.FINISHED_WITH_FAILURE;
		return this;
	}
	
	public Status status() {
		return this._status;
	}
	
	public String applyer() {
		return this._applyer;
	}
	
	public String target() {
		return this._target;
	}
}
